package com.tuanzi.loan.business.repository.banana.statistics.impl;

import com.tuanzi.loan.business.repository.banana.statistics.custom.BananaCommonActivityDeviceRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.banana.statistics.BananaCommonActivityDeviceRequest;
import com.tuanzi.loan.vo.response.banana.statistics.BananaCommonActivityDeviceResponse;
import com.tuanzi.server.loan_base.util.DateUtils;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.Date;

import static com.tuanzi.loan.generated.jooq.banana_stat.Tables.STAT_BANANA_ACTIVITY_DEVICE;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductChannel.COMM_PRODUCT_CHANNEL;

/**
 * 通用活动注册统计
 * 
 * @author yuzhenhua 2018/7/19
 */
@SuppressWarnings({ "rawtypes" })
@Repository
public class BananaCommonActivityDeviceRepositoryImpl extends JOOQBaseRepository implements BananaCommonActivityDeviceRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return STAT_BANANA_ACTIVITY_DEVICE;
	}

	@Override
	public Page<BananaCommonActivityDeviceResponse> list(BananaCommonActivityDeviceRequest searchRequest, Pageable pageable) {

		leftJoin(COMM_PRODUCT_CHANNEL, COMM_PRODUCT_CHANNEL.CHANNEL_CODE.eq(STAT_BANANA_ACTIVITY_DEVICE.CHANNEL));

		in(STAT_BANANA_ACTIVITY_DEVICE.PRODUCT_ID, searchRequest.getProductIds());
		if (searchRequest.getChannelCode() != null)
			if (!searchRequest.getChannelCode().contains("") || !searchRequest.getChannelCode().contains("9999"))
				in(STAT_BANANA_ACTIVITY_DEVICE.CHANNEL, searchRequest.getChannelCode());
		in(COMM_PRODUCT_CHANNEL.CHANNEL_GROUP, searchRequest.getChannelGroups());
		contains(STAT_BANANA_ACTIVITY_DEVICE.PLATFORM, searchRequest.getPlatform());
		// le(STAT_CHANNEL_UV.UV, searchRequest.getUvEnd());
		// ge(STAT_CHANNEL_UV.UV, searchRequest.getUvStart());

		between(STAT_BANANA_ACTIVITY_DEVICE.DATE,
				searchRequest.getStartDate() == null ? searchRequest.getStartDate() : DateUtils.formatDateToYyyyMMdd(searchRequest.getStartDate()),
				searchRequest.getEndDate() == null ? searchRequest.getEndDate() : DateUtils.formatDateToYyyyMMdd(searchRequest.getEndDate()));

		orderBy(STAT_BANANA_ACTIVITY_DEVICE.DATE, Sort.Direction.DESC);
		return search(pageable, record -> {

			BananaCommonActivityDeviceResponse response = new BananaCommonActivityDeviceResponse();
			response.setDate(record.get(STAT_BANANA_ACTIVITY_DEVICE.DATE));
			response.setPlatform(record.get(STAT_BANANA_ACTIVITY_DEVICE.PLATFORM));
			response.setBeforeAppNew(record.get(STAT_BANANA_ACTIVITY_DEVICE.BEFORE_APP_NEW));
			response.setBeforeAppApply(record.get(STAT_BANANA_ACTIVITY_DEVICE.BEFORE_APP_APPLY));
			response.setProductId(record.get(STAT_BANANA_ACTIVITY_DEVICE.PRODUCT_ID));
			response.setChannel(record.get(STAT_BANANA_ACTIVITY_DEVICE.CHANNEL));
			response.setBeforeAppBinding(record.get(STAT_BANANA_ACTIVITY_DEVICE.BEFORE_APP_BINDING));
			response.setBeforeAppCash(record.get(STAT_BANANA_ACTIVITY_DEVICE.BEFORE_APP_CASH));
			response.setBeforeAppLogin(record.get(STAT_BANANA_ACTIVITY_DEVICE.BEFORE_APP_LOGIN));
			response.setBeforeAppRegister(record.get(STAT_BANANA_ACTIVITY_DEVICE.BEFORE_APP_REGISTER));
			response.setBeforeAverageJoin(record.get(STAT_BANANA_ACTIVITY_DEVICE.BEFORE_AVERAGE_JOIN));
			response.setBeforeAverageQuota(record.get(STAT_BANANA_ACTIVITY_DEVICE.BEFORE_AVERAGE_QUOTA));

			return response;
		});
	}

	public Integer getUv(Date startTime, Integer channelId, Integer payMode) {
		// List<Integer> statCollaborateRegisterRecords = dslContext
		// .select(payMode == 1 ?
		// DSL.sum(STAT_COLLABORATE_REGISTER.APP_ACTIVATE_COUNT_PHONE) :
		// DSL.sum(STAT_COLLABORATE_REGISTER.EXTERNAL_NEW_REGISTER_COUNT))
		// .from(STAT_COLLABORATE_REGISTER).where(STAT_COLLABORATE_REGISTER.CDATE.eq(new
		// java.sql.Date(startTime.getTime())))
		// .and(STAT_COLLABORATE_REGISTER.CHANNEL.eq(channelId)).limit(1).fetchInto(Integer.class);
		// return statCollaborateRegisterRecords.isEmpty() == true ? 0 :
		// statCollaborateRegisterRecords.get(0) == null ? 0 :
		// statCollaborateRegisterRecords.get(0);

		return 0;
	}

	public Integer getRealCount(Date settlementTime, Integer channelId, Integer payMode) {
		// List<Integer> statCommActivityRegisterRecords =
		// dslContext.select(DSL.sum(STAT_COMM_ACTIVITY_REGISTER.APP_ACTIVATE_COUNT_PHONE)).from(STAT_COMM_ACTIVITY_REGISTER)
		// .where(STAT_COMM_ACTIVITY_REGISTER.CDATE.eq(new
		// java.sql.Date(settlementTime.getTime()))).and(STAT_COMM_ACTIVITY_REGISTER.CHANNEL.eq(channelId))
		// .groupBy(STAT_COMM_ACTIVITY_REGISTER.CHANNEL,
		// STAT_COMM_ACTIVITY_REGISTER.CDATE).limit(1).fetchInto(Integer.class);
		// Integer record = statCommActivityRegisterRecords.isEmpty() == true ? 0 :
		// statCommActivityRegisterRecords.get(0) == null ? 0 :
		// statCommActivityRegisterRecords.get(0);

		return 0;
	}

	public Integer getLoginRecord(Date date, Integer channel) {
		// sList<Integer> into =
		// dslContext.select(DSL.sum(STAT_COMM_ACTIVITY_REGISTER.LOGIN_RECORD_COUNT)).from(STAT_COMM_ACTIVITY_REGISTER)
		// .where(STAT_COMM_ACTIVITY_REGISTER.CDATE.eq(new
		// java.sql.Date(date.getTime()))).and(STAT_COMM_ACTIVITY_REGISTER.CHANNEL.eq(channel))
		// .groupBy(STAT_COMM_ACTIVITY_REGISTER.CHANNEL,
		// STAT_COMM_ACTIVITY_REGISTER.CDATE).fetchInto(Integer.class);
		// return into.isEmpty() ? 0 : into.get(0);

		return 0;
	}
}
